package com.flyxteam.commons.dao.mybatis.core.sqlbuilder;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.flyxteam.commons.dao.mybatis.api.condition.Condition;

import java.util.Set;

/**
 * UPDATE SQL构建器
 * @author yuzh
 *
 */
public class UpdateSqlBuilder extends ConditionSqlBuilder {

	/** 修改字段 */
	private Map<String, Object> updateColumns;
	
	public UpdateSqlBuilder(String tableName, Map<String, Object> updateColumns, List<Condition> conditionColumns) {
		this.tableName = tableName;
		this.updateColumns = updateColumns;
		this.conditionColumns = conditionColumns;
	}
	
	@Override
	public String buildSqlString() {
		// update tableName set column1 = value1, column2 = value2 where condition1 = conditionValue1 and condition2 = conditionValue2
		StringBuilder updateSql = new StringBuilder("update ");
		updateSql.append(tableName).append(" set ");
		
		Set<Entry<String, Object>> updateColumnEntrySet = updateColumns.entrySet();
		int i = 0;
		int updateColumnSize = updateColumns.size();
		for (Entry<String, Object> updateColumnEntry : updateColumnEntrySet) {
			updateSql.append(updateColumnEntry.getKey()).append(" = ").append("#{").append(updateColumnEntry.getKey()).append("_update_").append(i).append("} ");
			sqlParam.put(updateColumnEntry.getKey() + "_update_" + i, updateColumnEntry.getValue());
			if((i + 1) < updateColumnSize) {
				updateSql.append(", ");
			}
			
			i++;
		}
		
		String conditionSql = buildConditionSqlString();
		updateSql.append(conditionSql);
		
		return updateSql.toString();
	}

}
